
package com.compuvest.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.ServletConfig;

import com.compuvest.web.MySQL;

public class TabPage extends HttpServlet {

    private MySQL mysql;

//------------------------------------------------
//
    @Override
    public void init(ServletConfig config) throws ServletException {
	super.init(config);

        mysql = (MySQL)config.getServletContext().getAttribute("sb");
	if(mysql == null) {
            System.out.println("TabPage Error sb==null:");
        }
        System.out.println("TabPage init:");
    }

//------------------------------------------------
//
    @Override
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	doPost(request, response);
    }

//------------------------------------------------
//
    @Override
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        PrintWriter out = response.getWriter();
//System.out.print("tb="+request.getParameter("tb")+" iid="+request.getParameter("iid"));
        String tb = request.getParameter("tb");
	if(tb != null) {
            if("1".equals(tb)) {
                int itemID = 0;
                try { itemID = Integer.parseInt(request.getParameter("iid")); } catch(Exception ignore) { }

                StringBuilder line = new StringBuilder(1000);
                String desc = mysql.getItemDescription(itemID);
                if(desc != null) {
//                    line.append("<?xml version='1.0' encoding='UTF-8'?><name>aaaa</name>");
                    line.append(desc.replace("<XML id=\"descr\">", "").replace("</XML>", ""));
                }
//                else {
//                    line.append("No specifications available for this product at this time.<br/>");
//                }

                out.print(line.toString());
            }
            else {
                out.print("<?xml version='1.0' encoding='UTF-8'?><h1>" + tb + "</h1>");
            }
        }
    }

//------------------------------------------------
//
    @Override
    public void destroy() {
        mysql = null;
	System.out.println("TabPage Destroy:");
    }
}
